#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import time

import requests


def getMoreInfo(link):

    if "mid" not in link or "biz" not in link:
        print('短链，需要自行获取数值')
        res = requests.get(link,headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c1f) XWEB/11581 Flue",},verify=False)
        if res.status_code == 200:
            pattern = r'var\s+(biz|sn|mid|idx)\s*=\s*"([^"]+)"'
            matches = re.findall(pattern, res.text)
            # 将提取出来的值存储到单独的变量中
            for match in matches:
                if match[0]== 'biz':
                    _biz = match[1]
                elif match[0]=='sn':
                    sn = match[1]
                elif match[0]== 'mid':
                    mid = match[1]
                elif match[0]== 'idx':
                    idx = match[1]
            # 输出各个变量的值
            print("biz:", _biz)
            print("sn:",sn)
            print("'mid:", mid)
            print("idx:",idx)
    else:
        # 获得mid,_biz,idx,sn 这几个在link中的信息
        mid = link.split("&")[1].split("=")[1]
        idx = link.split("&")[2].split("=")[1]
        sn = link.split("&")[3].split("=")[1]
        _biz = link.split("&")[0].split("_biz=")[1]

    key = "daf9bdc5abc4e8d04f0ed8c33ccacfd6141d9a2a299e46adc72e2e2e0e6bbc78b78f1cee399472ce52469bac787acd124b108031e5c6ea6d103899d62516187b60fb942859e94a729010a3c1bf80a6c4295517817ccb05466d7864fdf544b471fe92f267bfc562c2a7b77b684bf3aada603542b675cda505546976bc12146ea5"  # 从fiddler 中获取
    # key = "daf9bdc5abc4e8d047f1290faaa2bcf8790ba6124a23dc100df3fb77c78e2b0a217c9911728c6864bfea35db1a654ff5aff6112e3d201bc24d2f244b366bdc3c4d0f4880a67b14df51f71f24c258319820c4517e2eb31765096d912170b8a8a4974b8f455a44a1ab19a60dccb5f74c7edc2456cc6299be6043599f4c7fa9c1e2"  # 从fiddler 中获取

    # 目标url
    url = "http://mp.weixin.qq.com/s"  # 获取详情页的网址
    # 添加Cookie避免登陆操作，这里的"User-Agent"最好为手机浏览器的标识
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c1f) XWEB/11581 Flue",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Dest": "document",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cookie": "rewardsn=; wxtokenkey=777; wxuin=6554925; devicetype=Windows11x64; version=63090c1f; lang=zh_CN; appmsg_token=1307_Gbv44rT1UMym4WwQkH5Pp0lk5qyn6z3vCUBOu51-JIlvIBpzGRZ9KQ-wC0MKssyZ6Y0a75O6ab8WHJsC; pass_ticket=J7YFXln7ctVuj8IPhMpYtc76M0CmAlgwTrbXswwAnCnjudq3VXWbs7bjnzMRExUE; wap_sid2=CK2KkAMSigF5X0hLdTJfTzNJemxCQUxtbnI3OG13ejlmd09RRnp0ZjhHR3JPc3E3cFNIaU1pYmlkaUlvbHlhUi04V25CblhfSlVlczBra3pxdFRicTV5RmVvREVkRk1BOGpHS1pNSG5ycG5Qck1DQl9FcGo5aE9RVWZaV0lDQzQ0LTFha2FZV3JxWGZnU0FBQX4wtYTtvAY4DUAB"
    }

    # params = {
    #     "__biz": "MzkwODc1NTQ0Ng==",
    #     "mid": "2247489332",
    #     "idx": "1",
    #     "sn": "9556d96e75fd676a450b5a60196c27b6",
    #     "chksm": "c0c47fadf7b3f6bbb3b8e93bbe0f25fc379c781a26aad92cd6cd7741be5c1a18713581ed4f53",
    #     "key": "daf9bdc5abc4e8d00843e8f1ce74509b0b21009931c336b3657c3b6d3b527d1f7736f259533207710a09e585a8d1a3d6007127e1d703ff102baf95ded3917586651c5f9d3259839e08f388a15a2102f35185132421196ee1fe675dc4f6b213733fa16d5b98bd50d8645c4176f569f31529f73cd642cc8e7d2dd1a06df9646d3c",
    #     "ascene": "1",
    #     "uin": "NjU1NDkyNQ==",
    #     "devicetype": "Windows 11 x64",
    #     "version": "63090c1f",
    #     "lang": "zh_CN",
    #     "countrycode": "CN",
    #     "exportkey": "n_ChQIAhIQXh4zshIb0YM0lwD04kd8qRLyAQIE97dBBAEAAAAAAIkmEVF0NewAAAAOpnltbLcz9gKNyK89dVj0U5%2FHCRRGaDMtqhHcvXc5kFu5qeupVVtGog9MXjbONKiUBjavPGCiKTxmddk1HhroAwYzO%2Bpjuz0Cvz%2Frk1xfXORMIx1HH3fjNQw8JTBSqXT1g5EBb9BpvlMdJWJJo6EwHVKvPf36I8lmVU%2BJlP2pnrTqCp6sSPFXXSQ7yElIETM26qjOBN6AG4nrSva6SV9E5EQssvrlhKk7%2Biuh0V9XlrmIlG4%2F%2BqOHsWSs6fJ4v25CzmITups49SmollHzQghrW7XR4bL8EpSGHuT5",
    #     "acctmode": "0",
    #     "pass_ticket": "azTod2%2FPiS8dVKnRDQ3CrJhCZoXgfOb%2FhzKbCwxh03gDkRQY1cdZyKSZjF5MnKXE",
    #     "wx_header": "1",
    #     "fasttmpl_type": "0",
    #     "fasttmpl_fullversion": "7572126-zh_CN-zip",
    #     "fasttmpl_flag": "1"
    # }

    params = {
        '__biz': _biz,
        'mid': mid,
        'idx': idx,
        'sn': sn,
        'key': key,
        # 'uin': uin,
        "uin": "NjU1NDkyNQ==",
        "pass_ticket": "azTod2%2FPiS8dVKnRDQ3CrJhCZoXgfOb%2FhzKbCwxh03gDkRQY1cdZyKSZjF5MnKXE",
        "chksm": "c0c47fadf7b3f6bbb3b8e93bbe0f25fc379c781a26aad92cd6cd7741be5c1a18713581ed4f53",
        "ascene": "1",
        "devicetype": "Windows 11 x64",
        "version": "63090c1f",
        "lang": "zh_CN",
        "countrycode": "CN",
        "exportkey": "n_ChQIAhIQXh4zshIb0YM0lwD04kd8qRLyAQIE97dBBAEAAAAAAIkmEVF0NewAAAAOpnltbLcz9gKNyK89dVj0U5%2FHCRRGaDMtqhHcvXc5kFu5qeupVVtGog9MXjbONKiUBjavPGCiKTxmddk1HhroAwYzO%2Bpjuz0Cvz%2Frk1xfXORMIx1HH3fjNQw8JTBSqXT1g5EBb9BpvlMdJWJJo6EwHVKvPf36I8lmVU%2BJlP2pnrTqCp6sSPFXXSQ7yElIETM26qjOBN6AG4nrSva6SV9E5EQssvrlhKk7%2Biuh0V9XlrmIlG4%2F%2BqOHsWSs6fJ4v25CzmITups49SmollHzQghrW7XR4bL8EpSGHuT5",
        "acctmode": "0",
        "wx_header": "1",
        "fasttmpl_type": "0",
        "fasttmpl_fullversion": "7572126-zh_CN-zip",
        "fasttmpl_flag": "1"
    }
    print(params)
    response = requests.get(url, headers=headers, params=params, verify=False)
    content = response.text
    with open('测试.html', 'w', encoding='utf-8') as f:
        f.write(content)
    # 使用正则表达式匹配 read_num 的值
    read_num = re.search(r"read_num:\s*'(\d+)'", content)
    old_like_count = re.search(r"old_like_count:\s*'(\d+)'", content)
    like_count = re.search(r"like_count:\s*'(\d+)'", content)
    share_count = re.search(r"share_count:\s*'(\d+)'", content)

    result = {
        "read_num": int(read_num.group(1)) if read_num else None,
        "old_like_count": int(old_like_count.group(1)) if old_like_count else None,
        "like_count": int(like_count.group(1)) if like_count else None,
        "share_count": int(share_count.group(1)) if share_count else None,
    }

    print(result)
    return result


getMoreInfo(
    "http://mp.weixin.qq.com/s?__biz=MzkwODc1NTQ0Ng==&mid=2247489257&idx=1&sn=a9eaedc5a7a8a497580e764e28002b44&chksm=c0c47e70f7b3f76687c33b02b008d3e272ac8ff6b727d77aa122d9292ef3fe0d2c5c054d05b6#rd")

# df = pd.read_csv("data.csv")
# try:
#     for index, row in enumerate(df.itertuples()):
#         readNum, likeNum, old_like_num = getMoreInfo(row.url)
#         df.loc[index, 'likeNum'] = likeNum
#         df.loc[index, 'readNum'] = readNum
#         df.loc[index, 'old_like_num'] = old_like_num
#         print(row.url)
#         # 歇3s，防止被封
#         time.sleep(3)
# except Exception as e:
#     print(e)
# df.to_csv("data1.csv")
